const cheerio = require('cheerio')

function Splider(url) {
    let scheam = url.slice(0, 5)

    scheam == 'http' ? request = require('http') : request = require('https')
    return new Promise((resolve, reject) => {
        request.get(url, res => {
            const statusCode = res.statusCode; //获取响应状态码
            const contentType = res.headers['content-type'];
            let data = ''
            let err = null;
            if (statusCode != 200) {
                err = new Error('状态错误')
            } else if (!/^text\/html/.test(contentType)) {
                err = new Error('类型错误')
            }
            if (err) {
                console.log(err);
                res.resume();
                return
            }
            res.on('data', chunk => {
                data += chunk.toString()
            })
            res.on('end', () => {
                resolve(data)
            })
        })
    }).then(data => {
        let $ = cheerio.load(data)
        return $
    })
}
module.exports = {
    Splider
}